#!/bin/bash

# 域名
domain='mogublog.cn'

# 安装软件包
yum install -y httpd php php-gd php-mysql php-pdo php-pecl-apc php-pecl-memcache php-xml mysql mysql-server memcached

# 配置文件
if [ ! -d 'conf_backup'  ];then
mkdir 'conf_backup'
fi

if [ -f '/etc/httpd.conf' ]; then
mv /etc/httpd.conf conf_backup
fi


########################  Apache  ########################


# 配置虚拟主机
mkdir -p /www/html

echo '

ServerTokens Prod
TraceEnable off

' >> /etc/httpd/conf/httpd.conf

echo '

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /www/html
    ServerName '$domain'
</VirtualHost>
<Directory "/www/html">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

' > /etc/httpd/conf.d/vhosts.conf

rm -f /etc/httpd/conf.d/welcome.conf

# 修改配置文件
sed -i 's/^#ServerName www.example.com:80/ServerName '$domain':80/' /etc/httpd/conf/httpd.conf
sed -i 's/^#LoadModule filter_module/LoadModule filter_module/' /etc/httpd/conf/httpd.conf

# 配置文件
ln -s /etc/httpd/conf/httpd.conf /etc/httpd.conf
ln -s /etc/httpd/conf.d/vhosts.conf /etc/vhosts.conf

# 设置防火墙开启80端口
if [ -z "`/sbin/iptables -nL | grep dpt:80`" ];then
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart
fi


########################  PHP  ########################


sed -i 's/;date.timezone =/date.timezone = PRC/g' /etc/php.ini


########################  Success  ########################

chcon -R -t httpd_user_content_t /www

setsebool -P httpd_can_network_connect true

# 开机启动
chkconfig httpd on
chkconfig mysqld on
chkconfig memcached on

# 启动
service httpd start
service mysqld start
service memcached start

echo "LAMP Install Success!Enjoy It!"
